package cn.yougou.common;

import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: DELL
 * @date: 2020/12/13 10:27
 * @description:
 */
@Component
public class AnalysisFileUtil {

    public AnalysisFileUtil() {

    }
    /**
     * 通过数组拼接成一个格式化的string
     * */
    public String returnStringByList(Integer[] list){
        if(list!=null){
            if(!list[0].equals("")){
                StringBuilder sb= new StringBuilder();
                for(int i=0;i < list.length; i++){
                    sb.append(list[i]+"#");
                }
                return sb.toString();
            }
        }
        return "";
    }
    /**
     * 通过文件路径返回 文件中的所有字符串
     * */
    public String returnReaderStr(String url){
        //读取文件到流 outinputstram
        FileReader fr =null;
        BufferedReader br = null;
        try {
            StringBuffer StrData = new StringBuffer();
            fr=new FileReader(url);
            br =new BufferedReader(fr);
            String  rlStr =null;
            while((rlStr = br.readLine()) != null){
            if(!rlStr.trim().equals("")){
                if(rlStr.indexOf("#")>(rlStr.length()-2)){
                    StrData.append( new String( rlStr.replace('#',' ').trim().getBytes("UTF-8"),"UTF-8"));
                }else if(rlStr.indexOf('#') ==0){
                    StrData.append( new String( rlStr.replace('#',' ').trim().getBytes("UTF-8"),"UTF-8")+"#");
                }else{
                    StrData.append( new String(rlStr.trim().getBytes("UTF-8"),"UTF-8")+"#");
                }

            }
            }

           // System.out.println(StrData.toString());
            return StrData.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }


    }
/**
 * 根据文件名和 String输出到文件中
 * */
        public void WriterFile(List<String> data,String url){
            FileWriter fw =null;
            BufferedWriter bw =null;
            try {
                bw= new BufferedWriter( new FileWriter(new File(url)));
                for(String item :data){
                    bw.write(item);
                 //   System.out.println(item);
                    bw.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    bw.flush();
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    /**
     *去掉重复的值
     */
    public List filtrateRepetition(List<String> str){
        List<String> strList = new ArrayList<String>();
        for(int i =0;i<str.size();i++){
            if(!strList.contains(str.get(i))){
                strList.add(str.get(i));
            }else{
                //  System.out.println("重复的值："+str.get(i)+"下标为："+i);
            }
        }
        // System.out.println(strList.size());
        return strList;
    }
    /**
     * 按照规定分割字符串 返回一个StringList
     * 例：输入1为从第0条数据开始没隔1条放入数组
     * */
    public List<String> strSplit(String data,int splitNumber){
        String splitSymbol ="#";
        String [] dataSplit=null;
        try {
             dataSplit =data.split(splitSymbol);
        }catch (NullPointerException e){
            dataSplit= new String []{""};
        }
        List<String> dataList = new ArrayList<String>();
        int startIndex =splitNumber-1;//起始下标
        int nextIndex = -1;//下一个下标
        int partition=splitNumber;//跳跃 因为有1的存在所以单独设置一下
        if(splitNumber ==1){
            partition =splitNumber+1;
        }
        for(int i =0;i<dataSplit.length;i++){
            if(i ==startIndex|| (nextIndex != -1 && i ==nextIndex)||splitNumber==0){
                dataList.add(dataSplit[i].trim());
                nextIndex=i+partition;
            }
        }
        return dataList;
    }
    /**
     * 获得一个指定行数并且无重复的StringList
     * */
    public List<String> getFileAssignLineList(String data,int splitNumber){
        return  filtrateRepetition( strSplit(data,splitNumber));
    }
    /**
     * 输出 在two 与 one 不同的信息
     * */
    public List equalsList(List<String> one,List<String> two){
        List<String> message= cloneList(two);
        //   int count =0;
        for(int o =0;o<one.size();o++){
            for(int t =0 ;t<two.size();t++){
                if(one.get(o).trim().equals(two.get(t).trim())){
                    //   System.out.println("one中没有的值为："+two.get(t)+"下标为："+t);
                    message.remove(one.get(o));
                    //  count++;
                    break;
                }
            }
        }
       /* System.out.println("two包含one的数量为："+count);
        System.out.println("two不包含one的数量为："+(two.size()-count));
        System.out.println("===========two中不包含的one==========");*/
        return message;
    }
    /**
     * 克隆一个数组
     * */
    public List cloneList(List data){
        List list = new ArrayList();
        for(int i=0;i<data.size();i++){
            list.add(data.get(i));
        }
        return list;
    }

    /**
     *list one 根据 two List 中的下标 删除 值
     * */
    public List deleteList(List one,List two){
        for(int t =0;t<two.size();t++){
            one.remove(Integer.parseInt(two.get(t).toString()));
        }
        return one;
    }
    /**
     * 返回 one list中 包含的two list的下标
     * */
    public List getListIndex(List one,List two){
        List indexList = new ArrayList();
        for(int o=0;o<one.size();o++){
            for(int t=0;t<two.size();t++){
                if(one.get(o).equals(two.get(t))){
                    //    System.out.println(two.get(t)+"的下标是："+o);
                    indexList.add(o);
                }
            }
        }
        return indexList;
    }

}
